﻿//using MFramework;
//using System.Collections.Generic;
//using UnityEngine;

//public class AudioManager : Singleton<AudioManager>
//{
//    private const int MUSIC_CHANNEL_COUNT = 4;
//    private const int SOUND_CHANNEL_COUNT = 8;

//    private static int globalSoundID;

//    public float GlobalMusicVolume
//    {
//        get
//        {
//            return m_musicVolumeFactor;
//        }
//        set
//        {
//            m_musicVolumeFactor = value; UpdateMusicVolume();
//        }
//    }
//    private float m_musicVolumeFactor = 1;

//    public float GlobalSoundVolume {
//        get 
//        { 
//            return m_soundVolumeFactor;
//        }
//        set
//        {
//            m_soundVolumeFactor = value; UpdateSoundVolume();
//        }
//    }
//    private float m_soundVolumeFactor = 1;

//    private AudioChannel[] m_musicChannels;
//    private AudioChannel[] m_soundChannels;

//    private Dictionary<int, Sound> m_soundDic;

//    protected override void Init()
//    {
//        m_musicChannels = new AudioChannel[MUSIC_CHANNEL_COUNT];
//        m_soundChannels = new AudioChannel[SOUND_CHANNEL_COUNT];
//        m_soundDic = new Dictionary<int, Sound>();

//        InitChannels();

//        MonoRuntime.Inst.Regist(MonoRuntime.MonoLifecycle.Update, Update);
//    }

//    private void InitChannels()
//    {
//        GameObject channelRoot = new GameObject("AudioChannelRoot");
//        Object.DontDestroyOnLoad(channelRoot);

//        GameObject musicChannelRoot = new GameObject("MusicChannel");
//        musicChannelRoot.transform.SetParent(channelRoot.transform);

//        for (int i = 0; i < MUSIC_CHANNEL_COUNT; ++i)
//        {
//            AudioChannel channel = musicChannelRoot.AddComponent<AudioChannel>();
//            channel.channelType = AudioChannel.ChannelType.Music;

//            m_musicChannels[i] = channel;
//        }

//        GameObject soundChannelRoot = new GameObject("SoundChannel");
//        soundChannelRoot.transform.SetParent(channelRoot.transform);

//        for (int i = 0; i < SOUND_CHANNEL_COUNT; ++i)
//        {
//            AudioChannel channel = soundChannelRoot.AddComponent<AudioChannel>();
//            channel.channelType = AudioChannel.ChannelType.SoundEx;

//            m_soundChannels[i] = channel;
//        }
//    }

//    public int PlayMusic(string path, bool loop = true, bool fadeIn = true, bool stopOther = true)
//    {
//        if (stopOther)
//        {
//            for(int i = 0; i < MUSIC_CHANNEL_COUNT; ++i)
//            {
//                if (m_musicChannels[i].IsPlaying())
//                {
//                    m_musicChannels[i].Stop(false);
//                }
//            }
//        }

//        AudioChannel channel = GetIdelMusicChannel();

//        if(channel == null)
//        {
//            Logcat.Print($"播放失败，没有闲置轨道：{path}", LogLevel.Warning);
//            return -1;
//        }

//        Sound sound = CreateSound(path, Sound.SoundType.Music, loop);

//        m_soundDic[sound.soundID] = sound;

//        channel.Play(sound, loop, fadeIn);

//        return sound.soundID;
//    }

//    public int PlaySoundEx(string path, bool loop = false, bool fadeIn = false)
//    {
//        AudioChannel channel = GetIdelSoundChannel();

//        if (channel == null)
//        {
//            Logcat.Print($"播放失败，没有闲置轨道：{path}", LogLevel.Warning);
//            return -1;
//        }

//        Sound sound = CreateSound(path, Sound.SoundType.SoundEx, loop);

//        m_soundDic[sound.soundID] = sound;

//        channel.Play(sound, loop, fadeIn);

//        return sound.soundID;
//    }

//    public void Resume(int soundID, bool fadeIn = false)
//    {
//        if (!m_soundDic.ContainsKey(soundID))
//        {
//            Logcat.Print($"ID: {soundID} Resume失败，sound不在缓存中，可能是由于ID错误或音频已被释放");
//            return;
//        }

//        Sound sound = m_soundDic[soundID];

//        AudioChannel channel = null;

//        if (sound.soundType == Sound.SoundType.Music)
//            channel = GetIdelMusicChannel();
//        else if (sound.soundType == Sound.SoundType.SoundEx)
//            channel = GetIdelSoundChannel();

//        if (channel == null)
//        {
//            Logcat.Print($"Resume失败，没有闲置轨道：{sound.clip}", LogLevel.Warning);
//            return;
//        }

//        channel.Play(sound, sound.loop, fadeIn);
//    }

//    public void Pause(int soundID, bool fadeOut = false)
//    {
//        if (!m_soundDic.ContainsKey(soundID)) return;

//        Sound sound = m_soundDic[soundID];

//        AudioChannel channel = GetChannel(sound);

//        if(channel != null)
//            channel.Stop(fadeOut);
//    }

//    public void Stop(int soundID, bool fadeOut = false)
//    {
//        if (!m_soundDic.ContainsKey(soundID)) return;

//        Sound sound = m_soundDic[soundID];

//        AudioChannel channel = GetChannel(sound);

//        if(channel != null)
//            channel.Stop(fadeOut);

//        m_soundDic.Remove(soundID);
//        ReferencePool.Release(sound);
//    }

//    public void StopAll(bool fadeOut = false)
//    {
//        foreach(Sound sound in m_soundDic.Values)
//        {
//            Stop(sound.soundID, fadeOut);
//        }
//    }

//    public float GetTimeline(int soundID)
//    {
//        if (!m_soundDic.ContainsKey(soundID))
//        {
//            Logcat.Print($"Sound不存在，ID:{soundID}", LogLevel.Error);
//            return -1;
//        }
//        Sound sound = m_soundDic[soundID];
//        return sound.timeline;
//    }

//    public void SetTimeline(int soundID, float timeline)
//    {
//        if (!m_soundDic.ContainsKey(soundID)) return;

//        Sound sound = m_soundDic[soundID];
//        AudioChannel channel = GetChannel(sound);

//        sound.timeline = timeline;

//        if(channel != null)
//            channel.SetTimeline(timeline);
//    }

//    public float GetVolume(int soundID)
//    {
//        if (!m_soundDic.ContainsKey(soundID))
//        {
//            Logcat.Print($"Sound不存在，ID:{soundID}", LogLevel.Error);
//            return -1;
//        }
//        Sound sound = m_soundDic[soundID];
//        return sound.volume;
//    }

//    public void SetVolume(int soundID, float volume)
//    {
//        if (!m_soundDic.ContainsKey(soundID)) return;

//        Sound sound = m_soundDic[soundID];
//        AudioChannel channel = GetChannel(sound);

//        sound.volume = volume;

//        if (channel != null)
//            channel.SetVolume(volume);
//    }

//    private void Update()
//    {
//        for (int i = 0; i < MUSIC_CHANNEL_COUNT; ++i)
//        {
//            m_musicChannels[i].UpdateTimeline();
//            //播放结束需要释放
//            if (!m_musicChannels[i].IsPlaying() && !m_musicChannels[i].IsIdle())
//            {
//                Stop(m_musicChannels[i].sound.soundID);
//            }
//        }
//        for (int i = 0; i < SOUND_CHANNEL_COUNT; ++i)
//        {
//            m_soundChannels[i].UpdateTimeline();
//            //播放结束需要释放
//            if (!m_soundChannels[i].IsPlaying() && !m_soundChannels[i].IsIdle())
//            {
//                Stop(m_soundChannels[i].sound.soundID);
//            }
//        }
//    }

//    private Sound CreateSound(string path, Sound.SoundType type, bool loop)
//    {
//        Sound sound = ReferencePool.Acquire<Sound>();
//        sound.soundID = ++globalSoundID;
//        sound.clip = ResourcesManager.LoadAsset<AudioClip>(path);
//        sound.soundType = type;
//        sound.loop = loop;
//        sound.volume = 1;
//        return sound;
//    }

//    private AudioChannel GetIdelMusicChannel()
//    {
//        for(int i = 0; i < MUSIC_CHANNEL_COUNT; ++i)
//        {
//            if (m_musicChannels[i].IsIdle())
//            {
//                return m_musicChannels[i];
//            }
//        }
//        return null;
//    }

//    private AudioChannel GetIdelSoundChannel()
//    {
//        for(int i = 0; i < SOUND_CHANNEL_COUNT; ++i)
//        {
//            if (m_soundChannels[i].IsIdle())
//            {
//                return m_soundChannels[i];
//            }
//        }
//        return null;
//    }

//    private AudioChannel GetChannel(Sound sound)
//    {
//        for (int i = 0; i < MUSIC_CHANNEL_COUNT; ++i)
//        {
//            if(!m_musicChannels[i].IsIdle() && m_musicChannels[i].sound.soundID == sound.soundID)
//            {
//                return m_musicChannels[i];
//            }
//        }
//        for (int i = 0; i < SOUND_CHANNEL_COUNT; ++i)
//        {
//            if(!m_soundChannels[i].IsIdle() && m_soundChannels[i].sound.soundID == sound.soundID)
//            {
//                return m_soundChannels[i];
//            }
//        }
//        return null;
//    }

//    private void UpdateMusicVolume()
//    {
//        for(int i = 0; i < MUSIC_CHANNEL_COUNT; ++i)
//        {
//            m_musicChannels[i].UpdateVolume();
//        }
//    }

//    private void UpdateSoundVolume()
//    {
//        for(int i = 0;i < SOUND_CHANNEL_COUNT; ++i)
//        {
//            m_soundChannels[i].UpdateVolume();
//        }
//    }
//}
